package com.doeis.uwork.dao;

import java.util.ArrayList;
import java.util.List;

import com.doeis.common.utils.StringUtils;
import com.doeis.uwork.entity.Item;
import com.google.inject.Singleton;
import com.voland.core.db.Dao;
import com.voland.core.db.DaoException;
import com.voland.core.db.Page;

@Singleton
public class ItemDao extends Dao {
	
	public Item findItemByid(Object id) throws DaoException{
		return findItemByid(id,false);
	}

	public Item findItemByid(Object id, boolean doAssociate) throws DaoException{
		try{
			return super.readById(Item.class, id, doAssociate);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 根据共享指数编号查询共享后的指数
	 * @param originalItemId
	 * @param doAssociate
	 * @return
	 * @throws DaoException
	 */
	public Item findItemByOriginalItemId(String originalItemId, boolean doAssociate) throws DaoException{
		try{
			return super.readByCondition(Item.class,  "id=?", doAssociate, originalItemId);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 根据共享指数编号查询共享后的指数列表
	 * @param originalItemId
	 * @return
	 * @throws DaoException
	 */
	public List<Item> findItemByOriginalItemId(String originalItemId) throws DaoException{
		try{
			String sql = " select * from item where original_item_id=? and status = true ";
			return query(Item.class, sql, originalItemId);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public Page<Item> listItems(Page page, boolean searchShared, Item itemForm, boolean doAssociate) throws DaoException{
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from item where status = true ");
			if(itemForm != null){
				if(!StringUtils.isBlank(itemForm.getSwitchboard_id())){
					sql.append("  and switchboard_id = ?");
					params.add(itemForm.getSwitchboard_id());
				}
				
				String userProductid = itemForm.getUser_product_id();
				if(!StringUtils.isBlank(userProductid)){
					sql.append(" and user_product_id = ? ");
					params.add( userProductid );
				}				
				
				if(!StringUtils.isBlank(itemForm.getUser_id())){
					sql.append(" and (user_id = ? or creator =?) ");
					params.add( itemForm.getUser_id() );
					params.add( itemForm.getUser_id() );
				}				
				
				String itemName = itemForm.getItem_name();
				if(!StringUtils.isBlank(itemName)){
					sql.append(" and item_name like ? ");
					params.add("%"+itemName+"%");
				}
				if(itemForm.getData_source() != null){
					sql.append(" and data_source = ? ");
					params.add(itemForm.getData_source());
				}
				if(!StringUtils.isBlank(itemForm.getItem_sign())){
					sql.append(" and item_sign = ? ");
					params.add(itemForm.getItem_sign());
				}
				
				if(!StringUtils.isBlank(itemForm.getProduct_id())){
					sql.append(" and product_id = ? ");
					params.add( itemForm.getProduct_id() );
				}	
				
				if(itemForm.getIs_top() != null){
					sql.append(" and is_top = ?");
					params.add(itemForm.getIs_top());
				}
				
				if (searchShared) {
					sql.append(" and (shared_item_id is not null or shared_out_item_id is not null ) ");
				}
				
				if(itemForm.getAttrs() != null ){
					sql.append(" and attrs like ? ");
					params.add("%"+itemForm.getAttrs()+"%");
				}
			}
			sql.append(" order by sort_num, updated_at desc ");
			 
			return super.querySlice(Item.class, sql.toString(), page, doAssociate, params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	/**
	 * 查询用户的条款
	 * @param item 查询条件
	 * @param showManual 当showManual不为空，并且item.getData_source()不为空时，用来区别是否只查手工录入还是非手工录入
	 * @return
	 * @throws DaoException
	 */
	public List<Item> findItems(Item item, boolean searchShared, Boolean showManual) throws DaoException {
		try{
			String switchboardid = item.getSwitchboard_id();
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from item where status = true");
			
			if(!StringUtils.isBlank(item.getSwitchboard_id())){
				sql.append(" and switchboard_id = ?");
				params.add(switchboardid);
			}
			
			if(!StringUtils.isBlank(item.getUser_product_id())){
				sql.append(" and user_product_id = ? ");
				params.add(item.getUser_product_id());
			}
			if(!StringUtils.isBlank(item.getId())){
				sql.append(" and id = ? ");
				params.add(item.getId());
			}
			
			if(item.getData_source() != null){
				if(showManual == null){
					sql.append(" and data_source = ? ");
					params.add(item.getData_source());
				}else{
					//由showManual来控制查询范围
					if(Boolean.TRUE.equals(showManual)){
						sql.append(" and data_source = ? ");
						params.add(item.getData_source());
					}else{
						sql.append(" and data_source <> ? ");
						params.add(item.getData_source());
					}
				}
			}
			if(!StringUtils.isBlank(item.getSource_id())){
				sql.append(" and source_id = ? ");
				params.add(item.getSource_id());
			}			
			
			if(!StringUtils.isBlank(item.getUser_id())){					
				sql.append(" and (user_id = ? or creator =?) ");
				params.add(item.getUser_id());
				params.add(item.getUser_id());
			}
			
			
			if(!StringUtils.isBlank(item.getCreator())){					
				sql.append(" and creator = ? ");
				params.add(item.getCreator());
			}
			if(searchShared){					
				sql.append(" and (shared_item_id is not null or shared_out_item_id is not null ) ");
			}
			
			return super.query(Item.class, sql.toString(), false, params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	
	/**
	 * 查询用户的条款,这个主要用户手机端
	 * @param item 查询条件
	 * @return
	 * @throws DaoException
	 */
	public List<Item> findItems(Item item,Boolean searchShared) throws DaoException {
		try{
			String switchboardid = item.getSwitchboard_id();
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from item where status = true");
			
			if(!StringUtils.isBlank(item.getSwitchboard_id())){
				sql.append(" and switchboard_id = ?");
				params.add(switchboardid);
			}
			
			if(!StringUtils.isBlank(item.getUser_id())){					
				sql.append(" and user_id = ?  ");
				params.add(item.getUser_id());
			}
			if(searchShared!=null){	
				if(searchShared){
					sql.append(" and (shared_item_id IS NOT NULL or shared_out_item_id IS NULL) ");
				}else{
					sql.append(" and shared_item_id IS NULL and shared_out_item_id IS NULL ");
				}
			}
			
			return super.query(Item.class, sql.toString(), false, params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	
	public List<Item> findItemsBySwitchboardid(String switchboardid) throws DaoException {
		try{
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql = new StringBuffer("select * from item");
			sql.append(" where switchboard_id = ? and status = true");
			params.add(switchboardid);
			return super.query(Item.class, sql.toString(), false, params.toArray());
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 *查询共享的一条条款 
	 * ***/
	public Item findOneShareItem() throws DaoException {
		try{
			return super.readByCondition(Item.class, " data_source =? and status = true", 1);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public Item findItemBySignName(String sign, String switchboard_id) throws DaoException {
		try{
			return super.readByCondition(Item.class, "item_sign = ? and switchboard_id =? ", sign, switchboard_id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 本方法只用于条款为系统共享条款的情况
	 * @param sign
	 * @return
	 * @throws DaoException
	 */
	public Item findItemBySignName(String sign) throws DaoException {
		try{
			return super.readByCondition(Item.class, "item_sign = ?", sign);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 取得系统当前最大的系统编号
	 * @return
	 * @throws DaoException
	 */
	public Item findMaxSystemItem()throws DaoException {
		try{
			String sql = "select * from item where item_sign like 's%' order by id desc";
			return super.read(Item.class, sql);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 取得系统当前最大的系统编号
	 * @return
	 * @throws DaoException
	 */
	public Item findMaxSystemOutItem()throws DaoException {
		try{
			String sql = "select * from item where item_sign like 'o%' order by id desc";
			return super.read(Item.class, sql);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	/**
	 * 查询所有ISP上共享的条款（以o开头的条款）
	 * @return
	 * @throws DaoException
	 */
	public List<Item> findSystemOutItems() throws DaoException {
		try{
			String sql = "select * from item where item_sign like 'o%' order by id desc";
			return super.query(Item.class, sql, false);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	/**
	 * 根据名字查询指数
	 * @param itemName
	 * @param switchboard_id
	 * @return
	 * @throws DaoException
	 * @author wxy
	 * 2014年10月13日星期一中国标准时间上午9:56:26
	 */
	public Item findItemByName(String itemName, String switchboard_id) throws DaoException {
		if (StringUtils.isBlank(itemName)) {
			return null;
		}
		try{
			return super.readByCondition(Item.class, "item_name =? and switchboard_id =?", itemName, switchboard_id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public Item readFirstItemByUesrProductId(String userProductid, String fromUserid) throws DaoException{
		try{
			return super.readByCondition(Item.class, "user_product_id =? and user_id =? and status = true order by id", userProductid,fromUserid);
		}catch(Exception e){
			throw new DaoException(e);
		}
	}
	
}